package org.jimm.util;

import java.util.Vector;

import org.jimm.app.R;

import android.content.Context;

public class Util {
    //private static Context mContext;

    public static final String LOG_TAG = "Jimm:Func";

    /*
      * String[] a = source.split(", ");
      */
    public static String[] explode(String text, char separator) {
        Vector<String> tmp = new Vector<String>();
        StringBuffer strBuf = new StringBuffer();
        int len = text.length();
        char chr;
        for (int i = 0; i < len; i++) {
            chr = text.charAt(i);
            if (chr == separator) {
                tmp.addElement(strBuf.toString());
                strBuf.delete(0, strBuf.length());
            } else
                strBuf.append(chr);
        }
        tmp.addElement(strBuf.toString());
        String[] result = new String[tmp.size()];
        tmp.copyInto(result);
        return result;
    }


    /**
     * The function checks whether the string in an array
     *
     * @param text String for search in array.
     * @param arr  array.
     * @return true if the string in the array
     */
    public static boolean CheckMass(String text, String[] arr) {
        boolean res = false;
        int strLenght = arr.length;
        for (int i = 0; i < strLenght; i++) {
            if (text.equals(arr[i])) {
                res = true;
                break;
            }
        }
        return res;
    }

    public static String WordEnd(int col, Context context) {
        // For the great and mighty Russian language. =)
        String lang = context.getResources().getConfiguration().locale.getLanguage();
        String wordend;
        if ("ru".equals(lang)) {
            int cols = col;
            wordend = String.valueOf(cols);
            int colend = Integer.parseInt(wordend);
            int numbers = colend % 10;
            switch (numbers) {
                case 1:
                    wordend = context.getText(R.string.msg_one).toString();
                    break;
                case 2:
                case 3:
                case 4:
                    wordend = context.getText(R.string.msg_several).toString();
                    break;
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 0:
                    wordend = context.getText(R.string.msg_many).toString();
                    break;

                default:
                    wordend = context.getText(R.string.msg_many).toString();
                    break;
            }
            if (colend < 15 & colend > 10) {
                wordend = context.getText(R.string.msg_many).toString();
            }

        }
        //for another lang
        else {
            if (col == 1) {
                wordend = context.getText(R.string.msg_one).toString();
            } else {
                wordend = context.getText(R.string.msg_many).toString();
            }
        }
        return wordend;
    }
}
